package design.patterns.iterator;

/**
 * @Desc:   具体聚集角色
 * @author: cww
 * @DateTime: 2020/6/12 19:40
 */

public class ConcreteAggregate extends Aggregate {

    private Object[] obj = {"M1","M2","M3","M4"};
    /**
     * 工厂方法，返回一个迭代子对象
     *
     * @return
     */
    @Override
    public Iterator createIterator() {
        return new ConcreteIterator();
    }

    private class ConcreteIterator implements Iterator{

        private int currentIndex = 0;

        /**
         * 迭代方法，移动到第一个元素
         */
        @Override
        public void first() {
            currentIndex = 0;
        }

        @Override
        public void next() {
            if (currentIndex < obj.length) {
                currentIndex++;
            }
        }

        @Override
        public boolean isDone() {
            return currentIndex == obj.length;
        }

        @Override
        public Object currentItem() {
            return obj[currentIndex];
        }
    }
}
